package com.woke.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.example.woke1.ZhongchouinfoActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Myzhongcdadter;
import com.woke.data.Datas_myzhongc1;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.xl.xxl.imagecache.ImageLoader;

public class Myzhong1Fragment extends Fragment implements OnItemClickListener{
	
	private View layout;
	private LayoutInflater minflater;
	private ListView mListView;
	private MyApp application;
	private ArrayList<Datas_myzhongc1> datalist = new ArrayList<Datas_myzhongc1>();
	private ArrayList<Datas_myzhongc1> newlist = new ArrayList<Datas_myzhongc1>();
	private ImageLoader imageLoader;
	private Myzhongcdadter adapter;
	private Datas_load dataload;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		if(layout == null){
			minflater = inflater;
			imageLoader = new ImageLoader(getActivity());
			layout = minflater.inflate(R.layout.item_singel_listview, null);
			intview();
		}else{
			ViewGroup parent = (ViewGroup) layout.getParent();
			if(parent!=null){
			parent.removeView(layout);
			}
		}
		return layout;
	}
	
	@Override
	public void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		super.onDestroy();
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		application = (MyApp) getActivity().getApplication();
		dataload = application.getDatas_load();
		adapter = new Myzhongcdadter(minflater, datalist, imageLoader,application.screenWidth);
		mListView.setAdapter(adapter);
		cxtchongzhi();
	}

	private void intview() {
		mListView = (ListView) layout.findViewById(R.id.listview_singel);
		mListView.setOnItemClickListener(this);
		
//		String getdataresu = Httpfgetdata.getdataresu();
//		Log.e("getdataresu", "&&"+getdataresu);
	}
	
	private void cxtchongzhi() {
		if(dataload==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(getActivity());    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "mycrowd");
		params.put("userid", dataload.getId());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("我的支持", "充值"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					JSONObject jsonObject = response.getJSONObject("data");
					if(flag.equals("success")){
						String str_result = jsonObject.getString("data");
						datalist.clear();
						newlist = (ArrayList<Datas_myzhongc1>) JSONArray.parseArray(str_result, Datas_myzhongc1.class);
						datalist.addAll(newlist);
						adapter.notifyDataSetChanged();
					}
					
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}



	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(getActivity(),ZhongchouinfoActivity.class);
		intent.putExtra("id", datalist.get(arg2).getCrowdid());
		intent.putExtra("myzhong", datalist.get(arg2).getStatus());
		startActivity(intent);
	}
}
